package com.fanhao.dllo.soundfm.utils;

import android.content.Context;
import android.os.Handler;
import android.support.v4.view.ViewPager;
import android.view.animation.Interpolator;
import android.widget.Scroller;

import java.lang.reflect.Field;

/**
 * Created by dllo on 15/11/21.
 */
public class ViewPagerScrollerMaster {


    private boolean turning;
    private boolean canTurn = false;
    private long autoTurningTime;
    private Handler handler = new Handler();
    private Runnable r;

    public void setRunnable(final ViewPager viewPager) {
        if (r == null) {
            r = new Runnable() {
                @Override
                public void run() {
                    if (viewPager != null) {
                        int page = viewPager.getCurrentItem();
                        viewPager.setCurrentItem(page);
                        handler.postDelayed(r, autoTurningTime);
                    }
                }
            };
        } else
            handler.postDelayed(r, autoTurningTime);
    }

    //     开始轮播
    public void startTurning(long autoTurningTime) {
        //如果是正在翻页的话先停掉
        if (turning) {
            stopTurning();
        }
        //设置可以翻页并开启翻页
        canTurn = true;
        this.autoTurningTime = autoTurningTime;
        turning = true;
        handler.postDelayed(r, autoTurningTime);
    }


    /**
     * 停止轮播
     */
    public void stopTurning() {
        turning = false;
        handler.removeCallbacks(r);
    }


    /**
     * 设置ViewPager的滑动速度
     */
    private void initViewPagerScroll(ViewPager viewPager) {
        try {
            Field mScroller = null;
            mScroller = ViewPager.class.getDeclaredField("mScroller");
            mScroller.setAccessible(true);
            ViewPagerScroller scroller = new ViewPagerScroller(
                    viewPager.getContext());
            mScroller.set(viewPager, scroller);
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }


    class ViewPagerScroller extends Scroller {
        private int mScrollDuration = 1200;// 滑动速度,值越大滑动越慢，滑动太快会使3d效果不明显

        public ViewPagerScroller(Context context) {
            super(context);
        }

        public ViewPagerScroller(Context context, Interpolator interpolator) {
            super(context, interpolator);
        }

        public ViewPagerScroller(Context context, Interpolator interpolator,
                                 boolean flywheel) {
            super(context, interpolator, flywheel);
        }

        @Override
        public void startScroll(int startX, int startY, int dx, int dy, int duration) {
            super.startScroll(startX, startY, dx, dy, mScrollDuration);
        }

        @Override
        public void startScroll(int startX, int startY, int dx, int dy) {
            super.startScroll(startX, startY, dx, dy, mScrollDuration);
        }

        public int getScrollDuration() {
            return mScrollDuration;
        }

        public void setScrollDuration(int scrollDuration) {
            this.mScrollDuration = scrollDuration;
        }

    }
}
